// EJFileView.cpp : implementation file
//

#include "stdafx.h"
#include "EJWork.h"
#include "EJFileView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEJFileView

IMPLEMENT_DYNCREATE(CEJFileView, CListView)

CEJFileView::CEJFileView()
{
}

CEJFileView::~CEJFileView()
{
}


BEGIN_MESSAGE_MAP(CEJFileView, CListView)
	//{{AFX_MSG_MAP(CEJFileView)
	ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
	ON_WM_MOUSEWHEEL()
	ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEJFileView drawing

void CEJFileView::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

/////////////////////////////////////////////////////////////////////////////
// CEJFileView diagnostics

#ifdef _DEBUG
void CEJFileView::AssertValid() const
{
	CListView::AssertValid();
}

void CEJFileView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEJFileView message handlers

void CEJFileView::OnInitialUpdate() 
{
	LOGFONT lf;

	CListView::OnInitialUpdate();
	
	CListCtrl& ctl = CListView::GetListCtrl();

	ctl.InsertColumn(0,"",LVCFMT_CENTER,78);
	ctl.InsertColumn(1,"",LVCFMT_CENTER,20);

	memset(&lf,0,sizeof(lf));
	strcpy(lf.lfFaceName,"Consolas");
	lf.lfHeight = 16;
	lf.lfWeight = FW_BOLD;
	m_font.CreateFontIndirect(&lf);
	ctl.SetFont(&m_font);
	
	Refresh();
}

BOOL CEJFileView::PreCreateWindow(CREATESTRUCT& cs) 
{
	cs.style |= (LVS_REPORT | LVS_SINGLESEL|LVS_SHOWSELALWAYS);
	return CListView::PreCreateWindow(cs);
}

void CEJFileView::Reload()
{
	WORD i;
	CString str;
	CListCtrl& ctl = CListView::GetListCtrl();
	
	ctl.DeleteAllItems();
	
	CEJData& d = theApp.ejd;
	for(i = 0;i<d.num;i++)
	{
		str.Format("%08ld",d.page[i].id);
		ctl.InsertItem(i,str);
	}
	
	ctl.SetItemState(0,LVIS_SELECTED,LVIS_SELECTED);
}

void CEJFileView::Refresh()
{
	WORD i;
	CListCtrl& ctl = CListView::GetListCtrl();

	ctl.SetItemState(theApp.pageInView,LVIS_SELECTED,LVIS_SELECTED);
	ctl.EnsureVisible(theApp.pageInView,TRUE);

	if (theApp.oper == OPER_CONTENT)
	{
		for(i = 0;i<theApp.ejd.num;i++)
		{
			if (theApp.ejd.page[i].num>0)
			{
				ctl.SetItemText(i,1,".");
			}
			else
			{
				ctl.SetItemText(i,1,"");
			}
		}
	}
	if (theApp.oper == OPER_CATALOG)
	{
		for(i = 0;i<theApp.ejd.num;i++)
		{
			if (theApp.ejd.page[i].pageID>0)
			{
				ctl.SetItemText(i,1,".");
			}
			else
			{
				ctl.SetItemText(i,1,"");
			}
		}
	}
}

void CEJFileView::OnClick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int index = -1;
	POSITION pos;
	CListCtrl& ctl = CListView::GetListCtrl();
	CEJData& ejd = theApp.ejd;
	CString str;
	CMainFrame* mf = (CMainFrame *)theApp.m_pMainWnd;
	CEJAnnoView* av = (CEJAnnoView *)mf->GetAnnoView();
	CEJEditView* ev = (CEJEditView *)mf->GetEditView();
	
	pos = ctl.GetFirstSelectedItemPosition();
	if (pos)
	{
		index = ctl.GetNextSelectedItem(pos);
		if (index>=0 && index<ejd.num)
		{
			theApp.pageInView = index;
			av->Refresh();
			ev->Refresh();
		}
	}

	*pResult = 0;	
}

BOOL CEJFileView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) 
{
	return CListView::OnMouseWheel(nFlags, zDelta, pt);
}

void CEJFileView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
{	
	int index = -1;
	POSITION pos;
	CListCtrl& ctl = CListView::GetListCtrl();
	CEJData& ejd = theApp.ejd;
	CString str;
	CMainFrame* mf = (CMainFrame *)theApp.m_pMainWnd;
	CEJAnnoView* av = (CEJAnnoView *)mf->GetAnnoView();
	CEJEditView* ev = (CEJEditView *)mf->GetEditView();
	CEJListView* lv = (CEJListView *)mf->GetListView();
	
	pos = ctl.GetFirstSelectedItemPosition();
	if (pos)
	{
		index = ctl.GetNextSelectedItem(pos);
		if (index>=0 && index<ejd.num)
		{
			theApp.pageInView = index;
			av->Refresh();
			if (theApp.oper == OPER_CATALOG)
			{
				lv->OnChange(theApp.pageInView);				
			}
		}
	}
	
	*pResult = 0;
}
